﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace igflow
{
    public static class igXmlLoadSave
    {
        public static void ReadXMLFile(string file)
        {
            //int level = 1;
            //string fullPath = Path.Combine(Path.Combine(StorageContainer.TitleLocation, "Levels"), string.Format("{0}.xml", level));  
            string path = "C:\\Projects\\igflow\\Project\\XML\\"; // caution need better way of handling this!!

            XmlDocument xmlDoc = new XmlDocument();

            if (File.Exists(path + file))
            {
                xmlDoc.Load(path + file);
                XmlNode root = xmlDoc.SelectNodes("Settings")[0];
                XmlElement rootElement = root as XmlElement;
                // Converts everything in the settings file to something usable
                foreach (XmlNode node in rootElement)
                {
                    XmlElement nodeElement = node as XmlElement;
                    if (nodeElement.Name == "ScreenWidth")
                    {
                        Game1.screenWidth = Int32.Parse(nodeElement.InnerText);
                    }
                    if (nodeElement.Name == "ScreenHeight")
                    {
                        Game1.screenHeight = Int32.Parse(nodeElement.InnerText);
                    }
                    if (nodeElement.Name == "ShowWindowsCursor")
                    {
                        Game1.showWindowsCursor = bool.Parse(nodeElement.InnerText);
                    }
                }

                xmlDoc = new XmlDocument();


                // Write down the XML declaration
                XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

                // Create the root element
                XmlElement rootNode = xmlDoc.CreateElement("CategoryList");
                xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
                xmlDoc.AppendChild(rootNode);

                // Create a new <Category> element and add it to the root node
                XmlElement parentNode = xmlDoc.CreateElement("Category");

                // Set attribute name and value!
                parentNode.SetAttribute("ID", "01");

                xmlDoc.DocumentElement.PrependChild(parentNode);

                // Create the required nodes
                XmlElement mainNode = xmlDoc.CreateElement("MainCategory");
                XmlElement descNode = xmlDoc.CreateElement("Description");
                XmlElement activeNode = xmlDoc.CreateElement("Active");

                // retrieve the text 
                XmlText categoryText = xmlDoc.CreateTextNode("XML");
                XmlText descText = xmlDoc.CreateTextNode("This is a list my XML articles.");
                XmlText activeText = xmlDoc.CreateTextNode("true");

                // append the nodes to the parentNode without the value
                parentNode.AppendChild(mainNode);
                parentNode.AppendChild(descNode);
                parentNode.AppendChild(activeNode);

                // save the value of the fields into the nodes
                mainNode.AppendChild(categoryText);
                descNode.AppendChild(descText);
                activeNode.AppendChild(activeText);

                // Save to the XML file
                xmlDoc.Save(path + "output.xml");


                MyTest testData = new MyTest();
                MyTest o = new MyTest();
                testData.other = o;

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;

                using (XmlWriter writer = XmlWriter.Create(path + "test.xml", settings))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(MyTest));
                    serializer.Serialize(writer, testData, null);
                }


            }
            else
                Console.WriteLine("The file {0} could not be located",
                                  file);


        }
    }
}
